﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookMS
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text!= "" && textBox2.Text!= "")
            {
                Login();
            }
            else
            {
                MessageBox.Show("输入有空项，请重新输入");
            }
        }
        //登录方法
        public void Login()
        {
            //用户
            if (radioButtonUser.Checked == true)
            {
                Dao dao = new Dao();
                //string  ="select * from t_user where id='"+textBox1.Text"' and psw='"+ textBox2.Text + "'";
                //string  = String.Format("select * from t_user where id='{0}' and psw='{1}'",textBox1.Text,textBox2.Text);
                string sql = $"select * from t_user where id='{textBox1.Text}' and psw='{textBox2.Text}'";
                IDataReader dc = dao.read(sql);
               
                if (dc.Read())
                {
                    Data.UID = dc["id"].ToString();
                    Data.UName = dc["name"].ToString();
                    MessageBox.Show("登录成功");
                    user1 user = new user1();
                    this.Hide();
                    user.ShowDialog();
                    this.Show();
                    
                }
                else
                {
                    MessageBox.Show("登录失败");
                }
                //MessageBox.Show(dc[0].ToString()+dc["name"].ToString);
            }
            //管理员
            if(radioButtonAdmin.Checked==true)
            {
                Dao dao = new Dao();
                //string  ="select * from t_user where id='"+textBox1.Text"' and psw='"+ textBox2.Text + "'";
                //string  = String.Format("select * from t_user where id='{0}' and psw='{1}'",textBox1.Text,textBox2.Text);
                string sql = $"select * from t_Admin where id='{textBox1.Text}' and psw='{textBox2.Text}'";
                IDataReader dc = dao.read(sql);

                if (dc.Read())
                {
                    MessageBox.Show("登录成功");
                    admin1 a = new admin1();
                    this.Hide();
                    a.ShowDialog();
                    this.Show();
                }
                else
                {
                    MessageBox.Show("登录失败");

                }
                dao.DaoClose();
            }

           
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void login_Load(object sender, EventArgs e)
        {

        }
    }
}
